﻿using System;
using System.Collections.Generic;
using System.Web;

namespace JinianNet.Web
{
    public class ResourceManager
    {
        public class Style
        {
            private static Dictionary<string, List<string>> dictionary;

            static Style()
            {
                dictionary = new Dictionary<string, List<string>>();
            }

            public static void Add(string key, params string[] value)
            {
                List<string> collections;
                if (dictionary.TryGetValue(key, out collections))
                {
                    foreach (string loop in value)
                    {
                        if (!JinianNet.Common.Utils.IsNullOrEmpty(loop) && !collections.Contains(loop))
                        {
                            collections.Add(loop);
                        }
                    }
                }
                else
                {
                    collections = new List<string>(value);
                }

                dictionary[key] = collections;
            }

            public static IHtmlString Render(params string[] collections)
            {
                List<string> value = new List<string>();
                foreach (string key in collections)
                {
                    List<string> list = new List<string>();
                    if (dictionary.TryGetValue(key, out list))
                    {
                        foreach (string loop in list)
                        {
                            if (!JinianNet.Common.Utils.IsNullOrEmpty(loop) && !value.Contains(loop))
                            {
                                value.Add(loop);
                            }
                        }
                    }
                }
                return new StyletHtmlString(value.ToArray());
            }

            public static IHtmlString Url(params string[] collections)
            {
                return new StyletHtmlString(collections);
            }
        }

        public class Script
        {
            private static Dictionary<string, List<string>> dictionary;

            static Script()
            {
                dictionary = new Dictionary<string, List<string>>();
            }

            public static void Add(string key, params string[] value)
            {
                List<string> collections;
                if (dictionary.TryGetValue(key, out collections))
                {
                    foreach (string loop in value)
                    {
                        if (!JinianNet.Common.Utils.IsNullOrEmpty(loop) && !collections.Contains(loop))
                        {
                            collections.Add(loop);
                        }
                    }
                }
                else
                {
                    collections = new List<string>(value);
                }

                dictionary[key] = collections;
            }

            public static IHtmlString Render(params string[] collections)
            {
                List<string> value = new List<string>();
                foreach (string key in collections)
                {
                    List<string> list = new List<string>();
                    if (dictionary.TryGetValue(key, out list))
                    {
                        foreach (string loop in list)
                        {
                            if (!JinianNet.Common.Utils.IsNullOrEmpty(loop) && !value.Contains(loop))
                            {
                                value.Add(loop);
                            }
                        }
                    }
                }
                return new ScriptHtmlString(value.ToArray());
            }

            public static IHtmlString Url(params string[] collections)
            {
                return new ScriptHtmlString(collections);
            }
        }

        public class ScriptHtmlString : IHtmlString
        {
            private string value;

            public ScriptHtmlString(params string[] keys)
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    if (!JinianNet.Common.Utils.IsNullOrEmpty(keys[i]))
                    {
                        keys[i] = string.Concat("<script type=\"text/javascript\" src=\"", keys[i], "\"></script>");
                    }
                }

                value = string.Join("\r\n", keys);
            }

            public string ToHtmlString()
            {
                return value;
                //return string.Concat("<script type=\"text/javascript\" src=\"",_url,"\"></script>");
            }

        }

        public class StyletHtmlString : IHtmlString
        {
            private string value;
            public StyletHtmlString(params string[] keys)
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    if (!JinianNet.Common.Utils.IsNullOrEmpty(keys[i]))
                    {
                        keys[i] = string.Concat("<link type=\"text/css\" rel=\"stylesheet\" href=\"", keys[i], "\" />");
                    }
                }

                value = string.Join("\r\n", keys);
            }

            public string ToHtmlString()
            {
                return value;
            }
        }
    }
}
